using UnityEngine;
using UnityEngine.UI;

// 示例：监听Model属性变化并更新UI
public class ModelListenerExample : MonoBehaviour
{
    [Header("UI组件")]
    public Text playerNameText;
    public Text levelText;
    public Text experienceText;
    public Slider healthSlider;
    public Slider experienceSlider;
    public Text musicVolumeText;
    public Slider musicVolumeSlider;
    
    private PlayerModel _playerModel;
    private GameSettingsModel _settingsModel;
    
    void Start()
    {
        // 获取Model实例
        _playerModel = ModelManager.GetModel<PlayerModel>();
        _settingsModel = ModelManager.GetModel<GameSettingsModel>();
        
        // 添加属性变化监听器
        _playerModel.AddPropertyChangedListener(OnPlayerModelChanged);
        _settingsModel.AddPropertyChangedListener(OnSettingsModelChanged);
        
        // 初始化UI
        UpdatePlayerUI();
        UpdateSettingsUI();
        
        // 设置UI控件的事件监听
        SetupUIEvents();
    }
    
    void OnDestroy()
    {
        // 移除监听器，避免内存泄漏
        if (_playerModel != null)
            _playerModel.RemovePropertyChangedListener(OnPlayerModelChanged);
        if (_settingsModel != null)
            _settingsModel.RemovePropertyChangedListener(OnSettingsModelChanged);
    }
    
    // 玩家Model属性变化回调
    private void OnPlayerModelChanged(ModelPropertyChangedEventArgs args)
    {
        switch (args.PropertyName)
        {
            case nameof(PlayerModel.Name):
                UpdatePlayerNameUI();
                break;
            case nameof(PlayerModel.Level):
                UpdateLevelUI();
                break;
            case nameof(PlayerModel.Experience):
                UpdateExperienceUI();
                break;
            case nameof(PlayerModel.Health):
                UpdateHealthUI();
                break;
        }
    }
    
    // 设置Model属性变化回调
    private void OnSettingsModelChanged(ModelPropertyChangedEventArgs args)
    {
        switch (args.PropertyName)
        {
            case nameof(GameSettingsModel.MusicVolume):
                UpdateMusicVolumeUI();
                break;
            case nameof(GameSettingsModel.SoundVolume):
                UpdateSoundVolumeUI();
                break;
            case nameof(GameSettingsModel.Fullscreen):
                UpdateFullscreenUI();
                break;
            case nameof(GameSettingsModel.QualityLevel):
                UpdateQualityUI();
                break;
        }
    }
    
    // 设置UI控件的事件监听
    private void SetupUIEvents()
    {
        if (musicVolumeSlider != null)
        {
            musicVolumeSlider.onValueChanged.AddListener(OnMusicVolumeSliderChanged);
        }
    }
    
    // 音乐音量滑块变化
    private void OnMusicVolumeSliderChanged(float value)
    {
        _settingsModel.MusicVolume = value;
    }
    
    // 更新玩家名称UI
    private void UpdatePlayerNameUI()
    {
        if (playerNameText != null)
        {
            playerNameText.text = $"玩家：{_playerModel.Name}";
        }
    }
    
    // 更新等级UI
    private void UpdateLevelUI()
    {
        if (levelText != null)
        {
            levelText.text = $"等级：{_playerModel.Level}";
        }
    }
    
    // 更新经验值UI
    private void UpdateExperienceUI()
    {
        if (experienceText != null)
        {
            experienceText.text = $"经验：{_playerModel.Experience}";
        }
        if (experienceSlider != null)
        {
            experienceSlider.value = _playerModel.ExperiencePercentage;
        }
    }
    
    // 更新生命值UI
    private void UpdateHealthUI()
    {
        if (healthSlider != null)
        {
            healthSlider.value = _playerModel.Health / 100f;
        }
    }
    
    // 更新音乐音量UI
    private void UpdateMusicVolumeUI()
    {
        if (musicVolumeText != null)
        {
            musicVolumeText.text = $"音乐音量：{_settingsModel.MusicVolume:P0}";
        }
        if (musicVolumeSlider != null)
        {
            musicVolumeSlider.value = _settingsModel.MusicVolume;
        }
    }
    
    // 更新音效音量UI
    private void UpdateSoundVolumeUI()
    {
        // 实现音效音量UI更新
    }
    
    // 更新全屏设置UI
    private void UpdateFullscreenUI()
    {
        // 实现全屏设置UI更新
    }
    
    // 更新画质设置UI
    private void UpdateQualityUI()
    {
        // 实现画质设置UI更新
    }
    
    // 更新所有玩家相关UI
    private void UpdatePlayerUI()
    {
        UpdatePlayerNameUI();
        UpdateLevelUI();
        UpdateExperienceUI();
        UpdateHealthUI();
    }
    
    // 更新所有设置相关UI
    private void UpdateSettingsUI()
    {
        UpdateMusicVolumeUI();
        UpdateSoundVolumeUI();
        UpdateFullscreenUI();
        UpdateQualityUI();
    }
    
    // 测试方法：模拟玩家数据变化
    [ContextMenu("测试玩家升级")]
    public void TestPlayerLevelUp()
    {
        _playerModel.AddExperience(150);
    }
    
    [ContextMenu("测试玩家受伤")]
    public void TestPlayerDamage()
    {
        _playerModel.TakeDamage(20f);
    }
    
    [ContextMenu("测试玩家治疗")]
    public void TestPlayerHeal()
    {
        _playerModel.Heal(30f);
    }
    
    [ContextMenu("测试设置变化")]
    public void TestSettingsChange()
    {
        _settingsModel.MusicVolume = Random.Range(0f, 1f);
        _settingsModel.QualityLevel = Random.Range(0, 6);
    }
} 